# -*- coding: utf-8 -*-

#===============================================================================
# GettingTasksDone.com project Routines file
# 2007-11-03: Sergeyev V.V.
#===============================================================================

from django import newforms as forms
from django.newforms import form_for_instance, form_for_model, save_instance, form_for_fields

from pds.models import *

from captcha.forms import CaptchaForm

#------------------------------------------------------------------------------ 
class RegistrationForm(forms.Form):
    username = forms.CharField(min_length=3, max_length=20)
    password = forms.CharField(widget=forms.PasswordInput)

#------------------------------------------------------------------------------ 
class TicketForm(CaptchaForm):
    username = forms.CharField(label="Your name", max_length=100,widget=forms.widgets.Input()) 
    comment =  forms.CharField(label="Description", widget=forms.widgets.Textarea())

#------------------------------------------------------------------------------ 
class UserForm(forms.Form):
    show_projects = forms.BooleanField()
    show_customers = forms.BooleanField()
    tasks_shared = forms.BooleanField()

#------------------------------------------------------------------------------ 
def item_field_callback(field, **kwargs):
    if isinstance(field, models.DateField):
        kwargs['widget'] = forms.TextInput(attrs={'class':'vDateField input', 'size': '15'})
    elif isinstance(field, models.FloatField):
        kwargs['widget'] = forms.TextInput(attrs={'class':'vFloatField'})
    elif isinstance(field, models.BooleanField):
        kwargs['widget'] = forms.CheckboxInput(attrs={'class':'vBooleanField'})
    
    return field.formfield(**kwargs)

#------------------------------------------------------------------------------ 
class Achivement(object):
    project = ""
    project_id = None
    time_spent = 0.0
    hour_rate = 0.0
    money_cost = 0.0
    first_task = None
    last_task = None
    percents = 0

    def __init__(self, project, project_id, hour_rate, time_spent, first_task, last_task):
        self.project = project
        self.project_id = project_id
        self.hour_rate = hour_rate
        self.time_spent = time_spent
        self.first_task = first_task
        self.last_task = last_task
    
    def __str__(self):
        return self.project
    
    def get_first_task(self):
        if self.first_task:
            return self.first_task
        else:
            return '...'
    
    def get_last_task(self):
        if self.last_task:
            return self.last_task
        else:
            return '...'

#------------------------------------------------------------------------------
def welcome_new_user(user):
    "Creates tasks for new user"
    
    new_project = Project(user=user, name="@Home", hour_rate=0.0, deleted=False)
    new_project.save()
    new_project = Project(user=user, name="@Office", hour_rate=0.0, deleted=False)
    new_project.save()
    new_project = Project(user=user, name="@Phone", hour_rate=0.0, deleted=False)
    new_project.save()
    
    new_task = Task(user=user, name="Define next actions", time_spent=0.0, text="All great achievements begins from small steps forward.", done=False, deleted=False)
    new_task.save()
    new_task = Task(user=user, name="Welcome to Getting tasks done!", time_spent=0.0, text="<-- double click checkbox to mark this task done", done=False, deleted=False)
    new_task.save()
#    new_task = Task(user=user, name="We have an Angel for Christmas tree?", time_spent=0.0, text="", done=False, deleted=False)
#    new_task.save()
#    new_task = Task(user=user, name="Don't forget about greeting cards!", time_spent=0.0, text="<-- double click checkbox to mark this task done", done=False, deleted=False)
#    new_task.save()

#------------------------------------------------------------------------------ 
def get_motivation():
    from random import randint as rint
    
    items_count = Motivation.objects.count()
    if items_count == 0:
        return None
    pk = rint(1, items_count)
    motivation = Motivation.objects.get(pk=pk)
    return motivation

#------------------------------------------------------------------------------ 
def get_adds():
    from random import randint as rint
    
    pk = rint(1, Adds.objects.count())
    motivation = Adds.objects.get(pk=pk)
    return motivation
                
#------------------------------------------------------------------------------ 
